import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
const $time 			= $Lazy.file(()=>import('@com/utils/time'))
const ControlModal 		= $Lazy.state(()=>import('./cpt/control-modal'))
const BillInfoModal 	= $Lazy.state(()=>import('./cpt/bill-info-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data: [],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		// 接收标本日期去掉时分秒  20211130 耿琰
		forms : [
			{ label: '账期',					name: 'bill_phase_id',			type: 'select', },
			{ label: '条码号',				name: 'spec_code' },
			{ label: '接收标本日期',			names: ['start_at','end_at'],	type: 'date-range', name: 'jz-time' },
			{ label: '账单状态',				name: 'status',					type: 'select', loading:true, idStr:'code',nameStr:'name' },
			{ label: '区域',					name: 'region_id',				type: 'select',	nameStr:'name' },
			{ label: '医院名称',				name: 'hosp_id',				type: 'select',	nameStr:'name'},
			{ label: '病人姓名 ',			name: 'patient_name' },
			{ label: '项目名称',				name: 'kind_id', type: 'select' },
			{ label: '自然项目 ',			name: 'parent_kind_id',			type: 'select',	nameStr:'name' },
			{ label: '采集类型 ',			name: 'collect_type',			type: 'select',	nameStr:'name' },
			{ label:' ', tag: '显示作废账单',	name: 'show_del', 	type:'checkbox', colon:false },
		],
	}
	
	model = {}
	query = { type:'my' }
	
	componentDidMount(){
		const { type } = this.props
		if(type){ this.query.type = type }
		this.getSelect()
		$time.then(f=>{
			const arr = f.duration['7-0-24']()
			this.resetModel = { start_at: arr[0], end_at: arr[1], 'jz-time':arr }
			this.model = {...this.model,...this.resetModel}
			if(window.isInit){
				this.fetch()
			}
		})
	}
	
	// 获取下拉数据
	getSelect = async () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 医院名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			// 区域
			$fn.setRow(forms, 'region_id', await f.getCache({ api:'bs_area' } ) )
			// 账期
			$fn.setRow(forms, 'bill_phase_id', await f.getCache({ api:'billPhaseSelect' } ) )
			// 采集类型
			$fn.setRow(forms, 'collect_type', await f.collect_type)
			//账单状态
			$fn.setRow(forms, 'status', await f.getDicCache({ code:45300 } ) )
			// 自然项目
			$fn.setRow(forms, 'parent_kind_id', await f.getCache({ api:'nature_project' } ) )
			// 项目名称
			$fn.setRow( forms, 'kind_id', await f.getCache({ api:'labkindselect' } ) )
			
			this.setState({ forms })
		})
		
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bill/salesmanauditlists',{isIndex:true})
	
	// 表单
	cols = [
		{ title: '账单编号', 		dataIndex: 'order_sn', 				width:200 },
		{ title: '条码号', 			dataIndex: 'old_spec_code', 		...$fn.table.spec_code },
		/* 我的财务账单添加字段显示第三方条码 2021-08-16  --耿琰 */
		{ title: '第三方条码', 		dataIndex: 'third_spec_code', 		width:120 },
		{ title: '接收标本日期', 		dataIndex: 'sp_check_time', 		...$fn.table.time },
		{ title: '开票单位', 		dataIndex: 'organization_name', 	width:150 },
		{ title: '开票单位编码', 		dataIndex: 'organization_code', 	width:150 },
		{ title: '姓名', 			dataIndex: 'patient_name', 			...$fn.table.name },
		{ title: '性别', 			dataIndex: 'sex_name', 				...$fn.table.sex },
		{ title: '年龄', 			dataIndex: 'age', 					...$fn.table.age },
		{ title: '所属医生', 		dataIndex: 'doctor', 				...$fn.table.name },
		{ title: '业务员名称', 		dataIndex: 'salesman_user', 		...$fn.table.name },
		{ title: '账期', 			dataIndex: 'phase_str', 			width:100, align:'center' },
		{ title: '自然项目', 		dataIndex: 'parent_kind_name', 		width:250 },
		{ title: '采集类型', 		dataIndex: 'collect_type_name', 	width:100 },
		{ title: '账单类型', 		dataIndex: 'attribute_name', 		width:100 },
		{ title: '医院名称', 		dataIndex: 'hosp_name', 			width:200 },
		{ title: '项目名称', 		dataIndex: 'item_name', 			width:250 },
		{ title: '标准价格', 		dataIndex: 'price', 				...$fn.table.num },
		{ title: '实际价格', 		dataIndex: 'sprice', 				...$fn.table.num },
		{ title: '折扣率', 			dataIndex: 'percent', 				...$fn.table.num },
		{ title: '折扣金额', 		dataIndex: 'perprice', 				...$fn.table.num },
		{ title: '账单生成时间', 		dataIndex: 'created_at', 			...$fn.table.time },
		{ title: '账单状态', 		dataIndex: 'status_name', 			width:150 },
		{ title: '备注', 			dataIndex: 'attribute_name', 		width:150 },
		TableControls.controls.call(this,{ width:160 })
	]
	// 表格操作按钮
	table_controls = (text, rows) => {
		return [
			TableControls.click.call(this, '账单信息', ()=> this.billInfoModalRef.open(rows) ),
			TableControls.click.call(this, '操作记录', ()=> this.controlModalRef.open(rows) )
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.exports.call(this, 'bill/salesmanauditlists', { isSock: 1 }), 	//  文件导出	
		]
	}
	render(){
		const { title } = this.props
		return (
			<>
				<Page title={ title ? title : '我的财务账单'} controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { timeStr:'7-0-24' }) }
					{/* 表格 */}
					{ Table.call(this,{type:1,isIndex:true,isInit: window.isInit}) }
				</Page>
				{/* 操作详情 */}
				<ControlModal onRef={ref=>this.controlModalRef=ref} />
				{/* 账单信息 */}
				<BillInfoModal onRef={ref=>this.billInfoModalRef=ref} />
			</>
		)
	}
}

export default Index